package com.monkey.web.platform.controller;

import com.monkey.core.domain.organization.Organization;
import com.monkey.core.plugin.dto.MonkeyDto;
import com.monkey.core.web.response.MonkeyResponseDto;
import com.monkey.web.platform.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Project  : monkey
 * Author   : Wu Tian Qiang
 * Date     : 2016/10/14 18:39:22
 */
@Controller
@RequestMapping(value = "/m", produces = {MediaType.APPLICATION_JSON_VALUE})
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    @RequestMapping(value = "/organization", method = RequestMethod.POST)
    public String addOrganization(MonkeyDto monkeyDto) {
        monkeyDto.println();
        organizationService.addOrganization(monkeyDto);
        return "SUCCESS:新增成功";
    }

    @RequestMapping(value = "/organization", method = RequestMethod.PUT)
    public String modifyOrganization(MonkeyDto monkeyDto) {
        monkeyDto.println();
        organizationService.modifyOrganization(monkeyDto);
        return "SUCCESS:修改成功";
    }

    @RequestMapping(value = "/organization/{ids}", method = RequestMethod.DELETE)
    public String removeOrganization(@PathVariable Long[] ids) {
        organizationService.removeOrganizations(ids);
        return "SUCCESS:删除成功";
    }

    @RequestMapping(value = "/child_organizations", method = RequestMethod.GET)
    public List<Organization> childOrganizations(@RequestParam Long parentId) {
        return organizationService.findChildByParent(parentId, false);
    }

    @RequestMapping(value = "/children_organizations", method = RequestMethod.GET)
    public MonkeyResponseDto childrenOrganizations(@RequestParam Long parentId) {
        MonkeyResponseDto responseDto = new MonkeyResponseDto("children");
        return responseDto.success(organizationService.findChildren(parentId, false).getChildren());
    }

    @RequestMapping(value = "/organization_list", method = RequestMethod.GET)
    public MonkeyResponseDto organizationList(@RequestParam Long parentId) {
        MonkeyResponseDto responseDto = new MonkeyResponseDto("children");
        return responseDto.success(organizationService.findChildren(parentId, true));
    }


}